{% extends 'index.html' %}
{% block page-content %}
<div id="page-wrapper">
    <div class="row">
         <div class="col-lg-12">
              <h1 class="page-header"><i class="fa fa-gears"></i><code>Ansible</code>模块管理</h1>
         </div>
                <!-- /.col-lg-12 -->
    </div>
	<div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
 						<i class="fa    fa-gears"></i>模块执行
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-lg-6">
                                <legend><i class="fa  fa-paper-plane"></i>Ansible临时任务</legend>
                                    <form role="form" id="ansibleModelRun" class="main form-horizontal" >{% csrf_token %}
										<fieldset>
											<div class="form-group">
												 <label class="col-sm-2 control-label">服务器</label>
												 <div class="col-sm-6">
		                                            <select multiple class="selectpicker" data-live-search="true" name="ansible_server"  id="ansible_server" required>
		                                            	{% for ds in serverList %}
		                                            		<option name="server" value="{{ds.id}}">{{ds.ip}}</option>
		                                            	{% endfor %}
		                                            </select>
		                                            <input  type="hidden"   class="form-control"  name="model" value="model">
		                                            <input  type="hidden"   class="form-control" id="ans_uuid" name="ans_uuid" value="{{ans_uuid}}">
												 </div>
											</div>
											<div class="form-group" >
													<label class="col-sm-2 control-label">模块</label>
													<div class="col-sm-6">
														<select class="form-control" name="ansible_model" id="ansible_model" onchange="javascript:oBtAnsibleModel();">
															<option value="ping" name="ansible_model">ping</option>
															<option value="raw" name="ansible_model">shell</option>
															<option value="yum" name="ansible_model">yum</option>
															<option value="copy" name="ansible_model">copy</option>
															<option value="service" name="ansible_model">service</option>
															<option value="file" name="ansible_model">file</option>
															<option value="user" name="ansible_model">user</option>
															<option value="synchronize" name="ansible_model">sync</option>
															<option value="cron" name="ansible_model">cron</option>
															<option value="get_url" name="ansible_model">wget</option>
															<option value="custom" name="ansible_model">自定义</option>
														</select>
													</div>
											</div>
											<div id="custom_model" class="form-group" style="display:none;">
												 <label class="col-sm-2 control-label">自定义模块</label>
												 <div class="col-sm-6">
													<input id="custom_model" type="text"   class="form-control" placeholder="输入模块" name="custom_model" value="">
												</div>
											</div>
											</fieldset>
											<div class="form-group">
												 <label class="col-sm-2 control-label">参数</label>
												 <div class="col-sm-6">
													<input id="ansible_args" type="text"   class="form-control" placeholder="输入模块参数" name="ansible_agrs" value="">
												</div>
											</div>
											</fieldset>
											<legend></legend>
											<div class="form-group">
												<label class="col-sm-3 control-label"></label>
												<button type="button"  onclick="repealFromVaule(this)" class="btn btn-default" >撤销</button>
										 		<button type="button"  class="btn btn-default" onclick="runAnsibleModel(this)" >执行</button>
									 		</div>
										</form>
                                </div>
                                <!-- /.col-lg-6 (nested) -->
                                <div class="col-lg-6">
                                    <legend><i class="fa  fa-paper-plane-o"></i>执行结果</legend>
									<pre>
				                        <div id="result">
				                        </div>
				                    </pre>
                                <!-- /.col-lg-6 (nested) -->
                            </div>
                            <!-- /.row (nested) -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
</div>

<script type="text/javascript">


function oBtAnsibleModel() {
	   var obj = document.getElementById("ansible_model");
	   var index = obj.selectedIndex;
	   var value = obj.options[index].value;
	   if (value=="raw"){
		   document.getElementById("ansible_args").value="uptime";
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if (value=="yum"){
		   document.getElementById("ansible_args").value="name=httpd state=present";
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if (value=="service"){
		   document.getElementById("ansible_args").value="name=httpd state=restarted enabled=yes";
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if (value=="cron"){
		   document.getElementById("ansible_args").value='name="sync time" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate window.time.com"';
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if (value=="file"){
		   document.getElementById("ansible_args").value='src=/root/test.txt dest=/tmp/test.txt owner=root group=root mode=700 state=touch';
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if (value=="copy"){
		   document.getElementById("ansible_args").value='src=/root/test.txt dest=/tmp/test.txt';
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if (value=="user"){
		   document.getElementById("ansible_args").value="name=welliam password='$6yshUMNL8dhY'";
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if (value=="synchronize"){
		   document.getElementById("ansible_args").value="src=/root/a dest=/tmp/ compress=yes rsync_opts=--exclude=.git";
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if (value=="get_url"){
		   document.getElementById("ansible_args").value="url=http://url/test.tar.gz dest=/tmp";
		   document.getElementById("custom_model").style.display = "none";
	   }
	   else if(value=="custom"){
		   document.getElementById("custom_model").style.display = "";
		   document.getElementById("ansible_args").value="";
	   }
	   else {
		   document.getElementById("ansible_args").value="";
	   }
}



	function runAnsibleModel(obj) {
		var btnObj = $(obj);
		btnObj.attr('disabled',true);
		var form = document.getElementById('ansibleModelRun');
		var post_data = {};
		for (var i = 1; i < form.length; ++i) {
			var name = form[i].name;
			var value = form[i].value;
			var project = name.indexOf("ansible_");
			if ( project==0 && value.length==0 && name!="ansible_agrs"){
				window.wxc.xcConfirm("请注意必填项不能为空~", window.wxc.xcConfirm.typeEnum.error);
				btnObj.removeAttr('disabled');
				return false;
			}
		};
		//alert(JSON.stringify(server_data));
		$("#result").html("服务器正在处理，请稍等。。。");
		/* 轮训获取结果 开始  */
	   var interval = setInterval(function(){
	        $.ajax({
	            url : '/apps/run/',
	            type : 'post',
	            data:$('#ansibleModelRun').serialize(),
	            success : function(result){
	            	if (result["msg"] !== null ){
	            		$("#result").append("<p>"+result["msg"]+"</p>");
	            		if (result["msg"].indexOf("[Done]") == 0){
	            			clearInterval(interval);
	            			window.wxc.xcConfirm("Ansible执行完成", window.wxc.xcConfirm.typeEnum.success);
	            			btnObj.removeAttr('disabled');
	            		}
	            	}
	            },
		    	error:function(response){
		    		btnObj.removeAttr('disabled');
		    		clearInterval(interval);
		    	}
	        });
	    },1000);

// 	    /* 轮训获取结果结束  */
		$.ajax({
			url:'/apps/model/', //请求地址
			type:"POST",  //提交类似
			data:$('#ansibleModelRun').serialize(),  //提交参数
			success:function(response){
				btnObj.removeAttr('disabled');
				if (response["code"] == "500"){
					clearInterval(interval);
					btnObj.removeAttr('disabled');
					window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.error);
				}

			},
	    	error:function(response){
	    		btnObj.removeAttr('disabled');
	    		window.wxc.xcConfirm("运行失败", window.wxc.xcConfirm.typeEnum.error);
	    		clearInterval(interval);
	    	}
		})
	}

{% if errorInfo %}
	window.wxc.xcConfirm("{{errorInfo}}", window.wxc.xcConfirm.typeEnum.error);
{% endif %}
</script>
</div>
{% endblock %}
